package com.yidu.ruifeng_logistics.service.impl;

import com.yidu.ruifeng_logistics.dao.LogisticsStorageDao;
import com.yidu.ruifeng_logistics.entity.LogisticsInStorage;
import com.yidu.ruifeng_logistics.dao.LogisticsInStorageDao;
import com.yidu.ruifeng_logistics.entity.LogisticsStorage;
import com.yidu.ruifeng_logistics.entity.LogisticsWaybill;
import com.yidu.ruifeng_logistics.service.LogisticsInStorageService;
import com.yidu.ruifeng_logistics.vo.LogisticsInStorageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 入库表(LogisticsInStorage)表服务实现类
 *
 * @author makejava
 * @since 2021-03-15 14:31:41
 */
@Service("logisticsInStorageService")
public class LogisticsInStorageServiceImpl implements LogisticsInStorageService {
    @Resource
    private LogisticsInStorageDao logisticsInStorageDao;

    @Resource
    private LogisticsStorageDao logisticsStorageDao;

    /**
     * 通过ID查询单条数据
     *
     * @param inStorageId 主键
     * @return 实例对象
     */
    @Override
    public LogisticsInStorage queryById(Integer inStorageId) {
        return this.logisticsInStorageDao.queryById(inStorageId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<LogisticsInStorage> queryAllByLimit(int offset, int limit) {
        return this.logisticsInStorageDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param logisticsInStorageVo 实例对象
     * @return 实例对象
     */
    @Override
    public LogisticsInStorage insert(LogisticsInStorageVo logisticsInStorageVo) {
        LogisticsInStorage logisticsInStorage = new LogisticsInStorage();
        BeanUtils.copyProperties(logisticsInStorageVo,logisticsInStorage);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if (StringUtils.hasText(logisticsInStorageVo.getInStorageGetTime())){
            LocalDateTime formatDateTime = LocalDateTime.parse(logisticsInStorageVo.getInStorageGetTime(), formatter);
            logisticsInStorage.setInStorageGetTime(formatDateTime);
        }
        if (StringUtils.hasText(logisticsInStorageVo.getInStorageOutTime())){
            LocalDateTime formatDateTime2 = LocalDateTime.parse(logisticsInStorageVo.getInStorageOutTime(), formatter);
            logisticsInStorage.setInStorageOutTime(formatDateTime2);
        }
        this.logisticsInStorageDao.insert(logisticsInStorage);
        return logisticsInStorage;
    }

    /**
     * 修改数据
     *
     * @param logisticsInStorageVo 实例对象
     * @return 实例对象
     */
    @Override
    public LogisticsInStorage update(LogisticsInStorageVo logisticsInStorageVo) {
        LogisticsInStorage logisticsInStorage = new LogisticsInStorage();
        //将vo复制到原生对象中
        BeanUtils.copyProperties(logisticsInStorageVo,logisticsInStorage);
        //用DateTimeFormatter.ofPattern格式化localdatetime为yyyy-MM-dd HH:mm:ss
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //判断时间字段是否为空
        if (StringUtils.hasText(logisticsInStorageVo.getInStorageGetTime())){
            //将日期字符串转化所需格式
            LocalDateTime formatDateTime = LocalDateTime.parse(logisticsInStorageVo.getInStorageGetTime(), formatter);
            //添加入库时间
            logisticsInStorage.setInStorageGetTime(formatDateTime);
        }
        //出库同理
        if (StringUtils.hasText(logisticsInStorageVo.getInStorageOutTime())){
            LocalDateTime formatDateTime2 = LocalDateTime.parse(logisticsInStorageVo.getInStorageOutTime(), formatter);
            logisticsInStorage.setInStorageOutTime(formatDateTime2);
        }
        this.logisticsInStorageDao.update(logisticsInStorage);
        return this.queryById(logisticsInStorage.getInStorageId());
    }

    /**
     * 通过主键删除数据
     *
     * @param inStorageId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer inStorageId) {
        return this.logisticsInStorageDao.deleteById(inStorageId) > 0;
    }

    @Override
    public List<LogisticsInStorageVo> queryAll(LogisticsInStorageVo logisticsInStorageVo) {
        return this.logisticsInStorageDao.queryAll(logisticsInStorageVo);
    }

    @Override
    public int deleteAll(String ids) {
        String[] s = ids.split(",");
        return logisticsInStorageDao.deleteAll(s);
    }

    /**
     * 根据运单生成入库信息
     * @param logisticsWaybill 运单对象
     * @return int
     */
    @Override
    public int addInStorage(LogisticsWaybill logisticsWaybill) {
        //new一个入库vo对象
        LogisticsInStorage logisticsInStorage = new LogisticsInStorage();
        //到达时间为现在时间
        logisticsInStorage.setInStorageGetTime(LocalDateTime.now());
        //出库时间为运单开始时间
        //logisticsInStorageVo.setInStorageOutTime(logisticsWaybill.getWaybillStartTime().toString());
        //上一站为运单Departure
        logisticsInStorage.setInStoragePrevious(logisticsWaybill.getWaybillDeparture());
        //下一站为运单Bourn
        logisticsInStorage.setInStorageNext(logisticsWaybill.getWaybillBourn());
        //添加运单id
        logisticsInStorage.setWaybillId(logisticsWaybill.getWaybillId());
        //new一个仓库对象
        LogisticsStorage logisticsStorage = new LogisticsStorage();
        //将仓库地址设置成运单Departure
        logisticsStorage.setStorageAddress(logisticsWaybill.getWaybillDeparture());
        //根据地址查询可用仓库
        List<LogisticsStorage> list = logisticsStorageDao.queryAll(logisticsStorage);
        //添加仓库id
        logisticsInStorage.setStorageId(list.get(0).getStorageId());
        //增加入库情况
        return logisticsInStorageDao.insert(logisticsInStorage);
    }

}
